/**
 * @作者 钟诚
 * @日期 2011-10-14
 * @时间 下午09:22:36
 * @描述
 * @版本
 */
package my.filter;

import java.io.IOException;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import my.commons.PagerManager;
import my.domain.Page;

/**
 * @作者 钟诚
 * @日期 2011-10-14
 * @时间 下午09:22:36
 * @描述
 * @版本 v1.0
 */
public class PageFilter implements Filter {
	private FilterConfig config = null;
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.Filter#destroy()
	 */
	public void destroy() {}

	/*
	 * (non-Javadoc)
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		Map<String, String> param = req.getParameterMap();
		for (String sname : param.keySet()) {
			System.out.println(sname);
		}
		String offset = req.getParameter("pager.offset");
		String count = req.getParameter("pager.count");
		String size = req.getParameter("pager.size");
		Page pager = PagerManager.getPager();
		pager=pager==null?new Page():pager;
		if (offset != null && !"".equals(offset)) {
			pager.setOffset(Integer.parseInt(offset));
		}
		if (count != null && !"".equals(count)) {
			pager.setCount(Integer.parseInt(count));
		}
		if (size != null && !"".equals(size)) {
			pager.setSize(Integer.parseInt(size));
		}
		PagerManager.setPager(pager);
		try {
			chain.doFilter(request, response);
			
		} finally {
			PagerManager.removePager();
		}
	}

	/*
	 * (non-Javadoc)
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	public void init(FilterConfig filterConfig) throws ServletException {
		this.config = filterConfig;
	}

}
